package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgHotWordBO;
import com.ztgf.shopping.models.bo.epg.EpgHotWordSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgHotWordDTO;
import com.ztgf.shopping.models.service.epg.IEpgHotWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * author: chenyu.wang
 * date: 2020-05-18 11:19
 * Description: 热词表控制器
 */
@RestController
@RequestMapping("/cms/epgHotWord")
@Slf4j
@Api(value = "EpgHotWordController", tags = "热词表接口")
public class EpgHotWordController {

    @DubboReference
    private IEpgHotWordService hotWordService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加热词表", notes = "添加热词表")
    public Result insert( EpgHotWordSaveBO hotWordSaveBO) {
        if(StrUtils.isEmpty(hotWordSaveBO.getHotWord())){
            return new Result(CodeEnum.FAIL_100007);
        }
        return hotWordService.add(hotWordSaveBO);
    }

    @ApiOperation(value = "修改热词表", notes = "修改热词表")
    @PostMapping("/update")
    public Result update( EpgHotWordSaveBO hotWordSaveBO) {
        String hotWord = hotWordSaveBO.getHotWord();
        if(StrUtils.isEmpty(hotWord)){
            return new Result(CodeEnum.FAIL_100008);
        }
        EpgHotWordDTO tempEpgHotWord = hotWordService.getByName(hotWord);
        if(null != tempEpgHotWord){
            return new Result(CodeEnum.FAIL_100032);
        }
        EpgHotWordDTO epgHotWordDTO = new EpgHotWordDTO();
        BeanUtils.copyProperties(hotWordSaveBO, epgHotWordDTO);
        epgHotWordDTO.setId(hotWordSaveBO.getId());
        boolean flag = hotWordService.updateById(epgHotWordDTO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<EpgHotWordDTO> get(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(hotWordService.selectById(id));
    }

    @ApiOperation(value = "删除热词表", notes = "删除热词表")
    @ApiImplicitParam(name = "id", value = "字典表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id) {
        boolean flag = hotWordService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除热词表", notes = "批量删除热词表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        hotWordService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @ApiOperation(value = "热词列表 倒序", notes = "热词列表 倒序")
    @PostMapping("/querySelectPageList")
    public Result<Page<EpgHotWordDTO>> querySelectPageList( EpgHotWordBO bo) {
        if(null == bo)
            return new Result<>(CodeEnum.FAIL_100002 );
        Page<EpgHotWordDTO> page = hotWordService.getBySelectHotInfoPage(bo);
        return new Result<>(page);
    }
}
